using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WODPL.GameComponents
{
    public class StubMapTileRenderModel : MapTileRenderModel
    {
        private readonly VertexPositionNormalTexture[] vertexData = new[]
                                                                  {
                                                                      new VertexPositionNormalTexture(
                                                                          new Vector3(-0.5f, 0, -0.5f),
                                                                          Vector3.Normalize(new Vector3(0, 1, 0)),
                                                                          Vector2.Zero),
                                                                      new VertexPositionNormalTexture(
                                                                          new Vector3(0.5f, 0, -0.5f),
                                                                          Vector3.Normalize(new Vector3(0, 1, 0)),
                                                                          Vector2.Zero),
                                                                      new VertexPositionNormalTexture(
                                                                          new Vector3(0.5f, 0, 0.5f),
                                                                          Vector3.Normalize(new Vector3(0, 1, 0)),
                                                                          Vector2.Zero),
                                                                      new VertexPositionNormalTexture(
                                                                          new Vector3(-0.5f, 0, 0.5f),
                                                                          Vector3.Normalize(new Vector3(0, 1, 0)),
                                                                          Vector2.Zero),
                                                                  };
        private readonly short[] indexData = new short[] { 0, 1, 2, 0, 2, 3 };
        private readonly BasicEffect basicEffect;
        private readonly GraphicsDevice graphicsDevice;

        public StubMapTileRenderModel(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;
            basicEffect = new BasicEffect(graphicsDevice, null) { DiffuseColor = new Vector3(0.5f, 0.7f, 0) };
            basicEffect.EnableDefaultLighting();
            basicEffect.PreferPerPixelLighting = true;
        }

        public BasicEffect BasicEffect
        {
            get { return basicEffect; }
        }

        public void Draw(Matrix projection, Matrix view, Matrix world)
        {
            graphicsDevice.RenderState.DepthBufferEnable = true;
            basicEffect.World = world;
            basicEffect.View = view;
            basicEffect.Projection = projection;

            graphicsDevice.RenderState.CullMode = CullMode.None;
            graphicsDevice.RenderState.FillMode = FillMode.Solid;
            graphicsDevice.VertexDeclaration = new VertexDeclaration(graphicsDevice, VertexPositionNormalTexture.VertexElements);
            basicEffect.Begin();
            foreach (var pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertexData, 0, 4, indexData, 0, 2);
                pass.End();
            }
            basicEffect.End();
        }
    }
}